package com.yangjie.autotest.hpsa.dao;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.yangjie.autotest.dao.AbstractHbnDao;
import com.yangjie.autotest.hpsa.domain.AuditMessage;
import com.yangjie.autotest.hpsa.domain.HisParentOrder;
import com.yangjie.autotest.hpsa.domain.ParentOrder;
import com.yangjie.autotest.hpsa.domain.ProvisionFile;

@Component
public class HPSADao extends AbstractHbnDao {
	public int getCurrentMaxOrderId() {
		Query query = getSession().createSQLQuery("select max(psid) from hgc_provision");
		List list = query.list();
		int maxPsid = 0;
		if (list != null) {
			if (((BigDecimal) list.get(0)) != null) {
				maxPsid = ((BigDecimal) list.get(0)).intValue();
			}
		}

		query = getSession().createSQLQuery("select max(psid) from hgc_provision_his");
		list = query.list();
		int hisMaxPsid = 0;
		if (list != null) {
			if (((BigDecimal) list.get(0)) != null) {
				hisMaxPsid = ((BigDecimal) list.get(0)).intValue();
			}
		}

		return maxPsid > hisMaxPsid ? maxPsid : hisMaxPsid;
	}

	public int getCurrentMaxAuditId() {
		Query query = getSession().createSQLQuery("select max(AUDIT_ID) from audit_record");
		List list = query.list();
		if (list == null) {
			return 0;
		}
		int maxAuditid = 0;
		if (((BigDecimal) list.get(0)) != null) {
			maxAuditid = ((BigDecimal) list.get(0)).intValue();
		}
		return maxAuditid;
	}

	public List<AuditMessage> getNewAuditMessage(int priMaxAuditId) {
		Query query = getSession().createSQLQuery("select * from audit_record where audit_id > " + priMaxAuditId).addEntity(AuditMessage.class);
		return query.list();
	}

	public List<ParentOrder> getNewParentORder(int priMaxOrderId) {
		// Query query =
		// getSession().createSQLQuery("select HGC_PROVISION_FILE.*, HGC_PROVISION.* from HGC_PROVISION left join HGC_PROVISION_FILE on HGC_PROVISION.fileid =HGC_PROVISION_FILE.fileid where psid > "
		// + priMaxOrderId).addEntity("order",
		// ParentOrder.class).addJoin("provisionFile", "order.provisionFile");
		Query query = getSession().createSQLQuery("select * from HGC_PROVISION  where Psid > " + priMaxOrderId).addEntity(ParentOrder.class);
		return query.list();
		// List resultList = query.list();
		// List<ParentOrder> retval = new LinkedList<ParentOrder>();
		// for(Object obj:resultList) {
		// Object[] tmp = (Object[])obj;
		// ParentOrder parentORder = (ParentOrder)tmp[0];
		// retval.add(parentORder);
		// }
		// return retval;
	}

	public List<HisParentOrder> getNewHisParentORder(int priMaxOrderId) {
//	Query query = getSession().createSQLQuery("select HGC_PROVISION_FILE.*, HGC_PROVISION_HIS.* from HGC_PROVISION_HIS left join HGC_PROVISION_FILE on HGC_PROVISION_HIS.fileid =HGC_PROVISION_FILE.fileid where psid > " + priMaxOrderId).addEntity("order", HisParentOrder.class).addJoin("provisionFile", "order.provisionFile");
		Query query = getSession().createSQLQuery("select *  from HGC_PROVISION_HIS  where psid > " + priMaxOrderId).addEntity(HisParentOrder.class);
		return query.list();
//		List resultList =  query.list();
//		List<HisParentOrder> retval = new LinkedList<HisParentOrder>();
//		for(Object obj:resultList) {
//			Object[] tmp = (Object[])obj;
//			HisParentOrder parentORder = (HisParentOrder)tmp[0];
//			retval.add(parentORder);
//		}
//		return retval;
	}
}
